import plotly.graph_objs as go
import pandas as pd
# Replace with the actual data from your file
data = {
'Year': [1997, 2001, 2005, 2009, 2014, 2018, 2022],
# Replace these example values with your actual district data
'Mayor election':[28.7109, 30.580032, 24.189132, 25.672194, 30.7377, 28.217202, 28.528908]}
# Add all other districts following the same structure
# Create a DataFrame from the data
df = pd.DataFrame(data)
# Create the interactive line chart
fig = go.Figure()
# Add a trace for each district
for column in df.columns[1:]: # Skipping 'Year'
fig.add_trace(go.Scatter(x=df['Year'], y=df[column], mode='lines+markers', name=column))
# Update the layout
fig.update_layout(
title='Taoyuan city Mayoral election data',
xaxis=dict(
title='Year',
tickmode='array', # Set tick mode to array to use custom tick values
tickvals=df['Year'], # Set custom tick values which are the years in your data
ticktext=df['Year'] # Set custom tick text which are the years in your data
),
yaxis=dict(title='Democracy index', fixedrange=True),
legend_title='District',
hovermode='x unified'
)
# Enable legend to hide/show traces
fig.update_layout(legend=dict(itemclick='toggleothers'))
# Show the figure
fig.show()